#include "ZoidbergState.h"
#include "MainMenuState.h"
#include "Game.h"
#include "SGD Wrappers\CSGD_TextureManager.h"
#include "SGD Wrappers\CSGD_DirectInput.h"
#include "SGD Wrappers\CSGD_XAudio2.h"
ZoidbergState* ZoidbergState::pInstance = nullptr;

ZoidbergState::ZoidbergState(void)
{
	Timer = -1;
	ZoidBGM = -1;
	LogoID = -1;
	Alpha = 1;
	CountDir = true;
	DrawLogo = true;
	PlayZoid = true;
	arcade = false;
}

ZoidbergState& ZoidbergState::operator=(const ZoidbergState&)
{
	return *this;
}

ZoidbergState::~ZoidbergState(void)
{
}

ZoidbergState* ZoidbergState::GetInstance(void)
{
	if(pInstance == nullptr)
		pInstance = new ZoidbergState();

	return pInstance;
}

void ZoidbergState::DeleteInstance()
{
	if(pInstance != nullptr)
	{
		delete pInstance;
		pInstance = nullptr;
	}
}

void ZoidbergState::Enter(void)
{
	Timer = 0.0f;
	PlayZoid = true;

	// Load in assets
	LogoID = CSGD_TextureManager::GetInstance()->LoadTexture(_T("resource/logo_team.png"));
	ZoidBGM = CSGD_XAudio2::GetInstance()->SFXLoadSound(_T("resource/soundFX/FX/Zoidberg.wav"));
}

bool ZoidbergState::Input(void)
{
	CSGD_DirectInput * di = CSGD_DirectInput::GetInstance();

	if(di->JoystickButtonPressed(0,0))
	{
		arcade = true;
	}

	if(di->JoystickButtonPressed(1,0))
	{
		arcade = true;
	}

	if(di->JoystickButtonPressed(2,0))
	{
		arcade = true;
	}

	if(di->JoystickButtonPressed(3,0))
	{
		arcade = true;
	}

	if(di->JoystickButtonPressed(4,0))
	{
		arcade = true;
	}

	if(di->JoystickButtonPressed(5,0))
	{
		arcade = true;
	}

	if(di->JoystickButtonPressed(6,0))
	{
		arcade = true;
	}

	if(CSGD_DirectInput::GetInstance()->KeyPressed(DIK_RETURN) || arcade)
	{
		CSGD_XAudio2::GetInstance()->SFXStopSound(ZoidBGM);
		Game::GetInstance()->ChangeState(MainMenuState::GetInstance());
	}
	return true;
}

void ZoidbergState::Update(float fElapsedTime)
{
	Timer += fElapsedTime; 
	if(Timer > 1.0f)
	{
		Timer = 0.0f;

		if(PlayZoid)
		{
			CSGD_XAudio2::GetInstance()->SFXPlaySound(ZoidBGM);
			PlayZoid = false;
		}
	}

	if(DrawLogo && Alpha >= 255)
	{
		CountDir = false;
	}
	else if(DrawLogo && Alpha <= 0)
	{
		DrawLogo = false;
		Game::GetInstance()->ChangeState(MainMenuState::GetInstance());
	}
	if(Timer > 0.05f)
	{
		if(DrawLogo && CountDir && Alpha < 255)
			Alpha += fElapsedTime * 120;
		else if(DrawLogo && !CountDir && Alpha > 0)
			Alpha -= fElapsedTime * 120;
	}
}

void ZoidbergState::Render(void)
{
	CSGD_TextureManager::GetInstance()->Draw(LogoID,-120,-200,1.0f,1.0f,nullptr,0.0f,0.0f,0.0f,D3DCOLOR_ARGB((int)Alpha,255,255,255));
}

void ZoidbergState::Exit(void)
{
	CSGD_XAudio2* XA = CSGD_XAudio2::GetInstance();
	
	if(ZoidBGM != -1)
	{
		XA->SFXStopSound(ZoidBGM);
		XA->SFXUnloadSound(ZoidBGM);
		ZoidBGM = -1;
	}

	DeleteInstance();
}